package com.clip.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.clip.entity.SystemDate;

@Repository
public class SystemDateDAO extends BasicDao {
	
	private static final Log log = LogFactory.getLog(SystemDateDAO.class);
	
	public static final String YEAR = "year";
	public static final String MONTH = "month";
	public static final String DAY = "day";
	public static final String LASTDAYS = "lastdays";
	public static final String NAME = "name";

	public void save(SystemDate transientInstance) {
		log.debug("saving SystemDate instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(SystemDate persistentInstance) {
		log.debug("deleting SystemDate instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SystemDate findById(java.lang.Integer id) {
		log.debug("getting SystemDate instance with id: " + id);
		try {
			SystemDate instance = (SystemDate) getSession().get(SystemDate.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SystemDate instance) {
		log.debug("finding SystemDate instance by example");
		try {
			List results = getSession().createCriteria(SystemDate.class).add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SystemDate instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from SystemDate as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByYear(Object year) {
		return findByProperty(YEAR, year);
	}

	public List findByMonth(Object month) {
		return findByProperty(MONTH, month);
	}

	public List findByDay(Object day) {
		return findByProperty(DAY, day);
	}

	public List findByLastdays(Object lastdays) {
		return findByProperty(LASTDAYS, lastdays);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findAll() {
		log.debug("finding all SystemDate instances");
		try {
			String queryString = "from SystemDate";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public SystemDate merge(SystemDate detachedInstance) {
		log.debug("merging SystemDate instance");
		try {
			SystemDate result = (SystemDate) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SystemDate instance) {
		log.debug("attaching dirty SystemDate instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SystemDate instance) {
		log.debug("attaching clean SystemDate instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	public List findByQuery(String query) {
		return getSession().createQuery(query).list();
	}
}